package com.soul.dubbo.api.db;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.soul.dubbo.api.mapper.UserMapper;
import com.soul.model.db.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@Transactional(propagation = Propagation.REQUIRED)
@DubboService
public class UserApiImpl implements UserApi{

    @Autowired
    private UserMapper userMapper;

    /**
     * 通过手机号码查询用户
     *
     * @param phone 手机号码
     * @return
     */
    @Override
    public User findByMobile(String phone) {
        // 方式一：
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(phone != null,User::getMobile,phone);
        // 方式二：
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(phone != null,"mobile",phone);
        return userMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 添加用户
     *
     * @param user 一条记录
     * @return
     */
    @Override
    public Long add(User user) {
        log.info("添加用户:{}",user);
        System.err.println("添加用户" + user);
        userMapper.insert(user);
        return user.getId();
    }

    /**
     * 更新用户
     *
     * @param user 用户对象
     * @return
     */
    @Override
    public void updateById(User user) {
        userMapper.updateById(user);
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }

    /**
     * 修改用户状态 用户状态,1为正常，2为冻结
     *
     * @param user
     */
    @Override
    public void updateUserStatus(User user) {
        userMapper.updateById(user);
    }
}
